package com.oren.base.service.impl;

import com.oren.base.annotation.Column;
import com.oren.base.annotation.Id;
import com.oren.base.annotation.TableName;
import com.oren.base.mapper.BaseMapper;
import com.oren.base.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: wangshiwei
 * Date: 2018/7/30
 * Time: 9:59
 * describe:
 */
@Service
@Transactional
public class BaseServiceImpl implements BaseService{

    private Logger logger= LoggerFactory.getLogger(BaseServiceImpl.class);


    @Autowired
    private BaseMapper baseMapper;


    @Override
    public void insertEntity(Object o) {

    }

    @Override
    public void deleteEntity(Integer id) {

    }

    @Override
    public void updateEntity(Object o) {

    }

    @Override
    public Object findEntity(Object o) {
        return null;
    }

    /**
     * 获取生成的map类型的insert语句
     * @param entity
     * @param <T>
     * @return
     */
    public <T> Map<String,Object> getInsertSql(T entity){
        Map<String,Object> map=new HashMap<String,Object>();
        if(entity.getClass().isAnnotationPresent(TableName.class)){
            logger.debug(entity.getClass()+"has no TableName annotation");
            throw new RuntimeException("Error for "+entity.getClass()+" Error @Table_name Annotation.");
        }else{
            StringBuilder sqlBuilder=new StringBuilder();//为了debug 输出日志，方便开发定位问题

            //获取表名
            String tableName="";
            TableName name=entity.getClass().getAnnotation(TableName.class);
            if(name!=null && !"".equals(name)){
                tableName=name.value();
            }else{
                tableName=entity.getClass().getSimpleName();
            }
            map.put("TABLE_NAME",tableName);
            sqlBuilder.append("create sql for : INSERT INTO "+tableName);

            LinkedList columnList=new LinkedList();//存储列名
            LinkedList valueList=new LinkedList();//存储值


            Field [] fields=entity.getClass().getDeclaredFields();
            try {
                for (Field field:fields) {
                    field.setAccessible(true);
                    if(field.isAnnotationPresent(Id.class)){
                        String idColumn="";
                        String idColumnValue=field.get(entity).toString();
                        if(idColumnValue!=null && !"".equals(idColumnValue)){
                            Id values=field.getAnnotation(Id.class);
                            if(!values.increase()){
                                if(values.value()!=null && !"".equals(values)){
                                    idColumn=values.value();
                                }else{
                                    idColumn=field.getName();
                                }

                                columnList.add(idColumn);
                                valueList.add(idColumnValue);
                            }
                        }
                    }else if(field.isAnnotationPresent(Column.class)){
                        String column="";
                        Column values=field.getAnnotation(Column.class);
                        if(values.value()!=null && !"".equals(values)){
                            column=values.value();
                        }else{
                            column=field.getName();
                        }
                        columnList.add(column);
                        valueList.add(field.get(entity));
                    }
                }
            }catch (Exception e){
                logger.error(" create insert sql failure "+e);
            }

        }

        return null;

    }
}
